//
//  PersonPannel.m
//  TipsProject
//
//  Created by Suny on 15/6/9.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "PersonPannel.h"
#import "UIImageView+WebCache.h"
#import "SegmentView.h"


@implementation TapItemView

- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self)
	{
		_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height/2.0)];
		_titleLabel.backgroundColor = [UIColor clearColor];
		_titleLabel.textAlignment = NSTextAlignmentCenter;
		_titleLabel.textColor = [UIColor whiteColor];
		[self addSubview:_titleLabel];
		
		
		_numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _titleLabel.bottom, self.width, self.height/2.0)];
		_numberLabel.backgroundColor = [UIColor clearColor];
		_numberLabel.textAlignment = NSTextAlignmentCenter;
		_numberLabel.textColor = [UIColor whiteColor];
		[self addSubview:_numberLabel];
		
		
		_maskImageView = [[UIImageView alloc] initWithFrame:self.bounds];
		
		[self addSubview:_maskImageView];
	}
	
	return self;
}


- (void)setSelected:(BOOL)selected
{
	_selected = selected;
	if (_selected)
	{
		_maskImageView.image = [[UIImage imageNamed:@"tap_item_view_mask"] 
								resizableImageWithCapInsets:UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f)];
	}
	else
	{
		_maskImageView.image = nil;
	}

}

+ (TapItemView *)itemWithTitle:(NSString *)title withNumber:(NSString *)number withFrame:(CGRect)rect
{
	TapItemView *item = [[TapItemView alloc] initWithFrame:rect];
	item.titleLabel.text = title;
	item.numberLabel.text = number;
	
	item.selected = NO;
	
	return item;
}

  

@end

@interface PersonPannel () <SegmentViewDelegate>
@property (nonatomic, strong) UIImageView *imageHeader;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIButton *attentionButton;

@property (nonatomic, strong) UIView *btnPannel;
@property (nonatomic, strong) UIImageView *userInfoView;
@property (nonatomic, strong) SegmentView *tipSegmentView;

@property (nonatomic, assign) NSInteger segmentIndex;
@property (nonatomic, strong) TapItemView *currentTapItemView;

@end
@implementation PersonPannel

- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	
	if (self)
	{

        [self addSubview:self.userInfoView];
		[self.userInfoView addSubview:self.imageHeader];
		[self.userInfoView addSubview:self.nameLabel];
		[self.userInfoView addSubview:self.btnPannel];
		self.userInfoView.image = [UIImage imageNamed:@"myprofile_header_background"];
		[self.userInfoView addSubview:self.attentionButton];
		self.attentionButton.hidden = YES;
		
		[self addSubview:self.tipSegmentView];
	}
	return self;
}



- (SegmentView *)tipSegmentView
{
	if (!_tipSegmentView)
	{
		_tipSegmentView = [[SegmentView alloc] initWithFrame:CGRectMake(20, self.userInfoView.bottom, self.width - 20 *2,
																		60) items:@[@"收藏",@"创建",@"喜欢"]];
		_tipSegmentView.tintColor = [UIColor lightGrayColor];
		
		_tipSegmentView.segmentDelegate = self;
	}
	
	return _tipSegmentView;
}

- (void)segmentViewSelectIndex:(NSInteger)index
{
	self.segmentIndex = index;
	if ([self.pannelDelegate respondsToSelector:@selector(personPannel:tapTipSegmentIndex:)])
	{
		[self.pannelDelegate personPannel:self tapTipSegmentIndex:index];
	}
	
}
- (UIImageView *)userInfoView
{
	if (!_userInfoView)
	{
		_userInfoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, 200)];
		_userInfoView.userInteractionEnabled = YES;
	}
	
	return _userInfoView;
}

- (void)setUserInfo:(UserInfo *)userInfo
{
	_userInfo = userInfo;
	
	[self.imageHeader setQNImageWithURL:[NSURL URLWithString:userInfo.headPhoto]
						placeholderImage:[UIImage imageNamed:kDefaultHeaderImage]];
	
	self.nameLabel.text = userInfo.nickName;
	
	TapItemView *item = (TapItemView *)[self.btnPannel viewWithTag:1000];
	item.numberLabel.text = userInfo.tipsNum;
	
	item = (TapItemView *)[self.btnPannel viewWithTag:2000];
	item.numberLabel.text = userInfo.followingNum;
	
	item = (TapItemView *)[self.btnPannel viewWithTag:3000];
	item.numberLabel.text = userInfo.followedNum;
	
	
	if (!self.currentTapItemView) 
	{
		[self tapItemView:(TapItemView *)[self.btnPannel viewWithTag:1000]];
	}
	
}

- (UIButton *)attentionButton
{
	if (!_attentionButton)
	{
		_attentionButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[_attentionButton setTitle:@"关注" forState:UIControlStateNormal];
		[_attentionButton setTitle:@"已关注" forState:UIControlStateSelected];
		_attentionButton.centerX = self.nameLabel.centerX;
		_attentionButton.top = self.nameLabel.bottom + 5;
	}
	
	return _attentionButton;
}

- (UIImageView *)imageHeader
{
	if (!_imageHeader)
	{
		_imageHeader = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
		_imageHeader.centerX = self.userInfoView.width/2.0;
		_imageHeader.bottom = self.userInfoView.height/2.0;
		_imageHeader.layer.cornerRadius = _imageHeader.width/2.0;
		_imageHeader.layer.borderWidth = 2.0;
		_imageHeader.layer.borderColor = [ UIColor whiteColor].CGColor;
		_imageHeader.clipsToBounds = YES;
	}
	
	return _imageHeader;
}

- (UILabel *)nameLabel
{
	if (!_nameLabel)
	{
		_nameLabel = [[UILabel alloc] init];
		_nameLabel.backgroundColor = [UIColor clearColor];
		_nameLabel.font = [UIFont systemFontOfSize:16];
		_nameLabel.frame = CGRectMake(50, self.imageHeader.bottom + 10, self.width - 100, 30);
		_nameLabel.textColor = [UIColor whiteColor];
		_nameLabel.textAlignment = NSTextAlignmentCenter;
		_nameLabel.userInteractionEnabled = YES;
	}
	
	return _nameLabel;
}


- (UIView *)btnPannel
{
	if (!_btnPannel)
	{
		_btnPannel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 40)];
		TapItemView *item = [TapItemView itemWithTitle:@"技巧" 
											withNumber:@"0"
											 withFrame:CGRectMake(0, 0, _btnPannel.width/3.0, _btnPannel.height)];
		item.tag = 1000;
		
		UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
																					 action:@selector(handleTapItemViewGesture:)];
		tapGesture.numberOfTapsRequired = 1;
		
		[item addGestureRecognizer:tapGesture];
		
		[_btnPannel addSubview:item];
		
		
		UIImageView *lineVertical = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0.5, _btnPannel.height)];
		lineVertical.image = [UIImage imageNamed:@"segment_line_vertical"];
		lineVertical.left = item.right;
		[_btnPannel addSubview:lineVertical];

		item = [TapItemView itemWithTitle:@"关注" withNumber:@"0" withFrame:CGRectMake( _btnPannel.width/3.0, 0, _btnPannel.width/3.0, _btnPannel.height)];
		item.tag = 2000;
		
		tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self 
															 action:@selector(handleTapItemViewGesture:)];
		tapGesture.numberOfTapsRequired = 1;
		
		[item addGestureRecognizer:tapGesture];
		item.userInteractionEnabled = YES;
		[_btnPannel addSubview:item];
		
		UIImageView *lineVertical1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0.5, _btnPannel.height)];
		lineVertical1.image = [UIImage imageNamed:@"segment_line_vertical"];
		lineVertical1.left = item.right;
		[_btnPannel addSubview:lineVertical1];
		
		item = [TapItemView itemWithTitle:@"粉丝" withNumber:@"0" withFrame:CGRectMake(_btnPannel.width/3.0*2.0, 0, _btnPannel.width/3.0, _btnPannel.height)];
		item.tag = 3000;
		
		tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapItemViewGesture:)];
		tapGesture.numberOfTapsRequired = 1;
		
		[item addGestureRecognizer:tapGesture];
		
		[_btnPannel addSubview:item];
		
		

		
		_btnPannel.bottom = self.userInfoView.bottom;
		
		
		item = (TapItemView *)[self.btnPannel viewWithTag:1000];

		[self tapItemView:item];
		
	}
	
	
	return _btnPannel;
}


- (void)handleTapItemViewGesture:(UIGestureRecognizer *)tapGesture
{
	[self tapItemView:(TapItemView *)tapGesture.view];

}

- (void)tapItemView:(TapItemView *)tapItemView
{
	TapItemView *item = (TapItemView *)[self.btnPannel viewWithTag:1000];
	item.selected = NO;
	
	item = (TapItemView *)[self.btnPannel viewWithTag:2000];
	item.selected = NO;
	
	item = (TapItemView *)[self.btnPannel viewWithTag:3000];
	item.selected = NO;
	
		
	tapItemView.selected = !tapItemView.selected;
	_currentTapItemView = tapItemView;

	
	if ([self.pannelDelegate respondsToSelector:@selector(personPannel:tapItem:)])
	{
		[self.pannelDelegate personPannel:self tapItem:tapItemView];
	}

    if (tapItemView.tag == 1000)
    {
        [self showTipSegmentView:YES];
    }
    else
    {
        [self showTipSegmentView:NO];

    }

}


- (void)showTipSegmentView:(BOOL)show
{
    self.tipSegmentView.hidden = !show;
	
}

- (PersonPannelTapState)state
{
	return self.currentTapItemView.tag;
}


@end
